package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.ZhongBaoWeixinIndexService;
import org.itboys.mobile.service.mongo.weixin.ZhongBaoWeiXinMessageService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 作者：江雄
 * 时间：2017年5月9日
 * 描述：众包_微信_首页_controller
 */
@Controller
@RequestMapping("/zhongbao/weixin/index")
public class ZhongBaoWeiXinIndexController extends BaseController{
    @Autowired
    private ZhongBaoWeixinIndexService indexService;


    /**
     * 首页
     * @param request
     * @param response
     */
    @RequestMapping(value="", method= RequestMethod.GET)
    @ApiOperation(value="首页.统计：项目申请未读总数，报名申请未读，留言查看未读")
    public String index(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
            ){
        try{
            Map<String,Object> result= indexService.getIndexData(request,AccountIdHoldUtils.getSessionMember(request).getId());
            model.addAttribute("result",result);
            return "/zhongbao/weixin/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/zhongbao/weixin/index----e="+e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }


}
